package com.project.mvprxsample.ui.activity;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.project.mvprxsample.R;
import com.project.mvprxsample.app.AppManager;
import com.project.mvprxsample.utils.SysConfig;

public abstract class BaseActivity extends AppCompatActivity  {

    public Context mContext;

    private LayoutInflater mLayoutInflater;
    private LinearLayout mContainer;
    private TextView mBack;
    private ImageView mActionMenu;
    private TextView mActionText;
    private TextView mTitle;
    public SysConfig mSysConfig;
    public LinearLayout mActionLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mContext = this;
        mSysConfig = SysConfig.getConfig(getApplicationContext());
        super.onCreate(savedInstanceState);
        mLayoutInflater = LayoutInflater.from(this);
    }

    @Override
    public void setContentView(int layoutResID) {
        mContainer = (LinearLayout) mLayoutInflater.inflate(R.layout.activity_base, null);
        mBack = (TextView) mContainer.findViewById(R.id.up);
        mTitle = (TextView) mContainer.findViewById(R.id.title);
        mActionMenu = (ImageView) mContainer.findViewById(R.id.action_menu);
        mActionText = (TextView) mContainer.findViewById(R.id.action_text);
        Drawable iconActionMenu = getActionMenuIcon();
        if (iconActionMenu != null) {
            mActionMenu.setVisibility(View.VISIBLE);
            mActionMenu.setImageDrawable(iconActionMenu);
        }
        String rightText = getActionMenuText();
        if (rightText != null) {
            mActionText.setVisibility(View.VISIBLE);
            mActionText.setText(rightText);
        }
        mBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mActionLayout = (LinearLayout) mContainer.findViewById(R.id.actionLayout);
        mActionLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                doActionMenu();
            }
        });
        FrameLayout content = (FrameLayout) mContainer.findViewById(R.id.content);
        if (hideActionBar()) {
            mContainer.findViewById(R.id.actionbar).setVisibility(View.GONE);
        }
        mLayoutInflater.inflate(layoutResID, content);
        getWindow().setContentView(mContainer);
        mTitle.setText(getTitle());

        initPresenters();
        initView();
        initTitle();
    }

    public void setActionMenuText(String text) {
        if (mActionText != null) {
            mActionText.setVisibility(View.VISIBLE);
            mActionText.setText(text);
        }
    }

    public void setActionMenuText(int id) {
        if (mActionText != null) {
            mActionText.setVisibility(View.VISIBLE);
            mActionText.setText(id);
        }
    }

    /**
     * 隐藏标题栏右边的文字
     */
    public void hideActionMenuText() {
        mActionText.setVisibility(View.GONE);
    }

    public void hideActionIcon() {
        mActionMenu.setVisibility(View.GONE);
    }

    /**
     * 设置右部点击事件是否可用 并且改变文字的颜色
     *
     * @param isEnable
     */
    public void setActionMenuTextEnable(boolean isEnable) {
        if (!isEnable) {
            mActionLayout.setEnabled(false);
            mActionText.setTextColor(ContextCompat.getColor(mContext, R.color.line));
        } else {
            mActionLayout.setEnabled(true);
            mActionText.setTextColor(mContext.getResources().getColor(R.color.white));
        }
    }


    public void setActionMenuIcon(Drawable icon) {
        if (mActionMenu != null) {
            if (icon == null) {
                mActionMenu.setVisibility(View.GONE);
            } else {
                mActionMenu.setVisibility(View.VISIBLE);
                mActionMenu.setImageDrawable(icon);
            }
        }
    }


    /**
     * 初始化标题栏
     */
    protected abstract void initTitle();

    /**
     * 初始化控件
     */
    protected abstract void initView();

    /**
     * 初始化presenters
     */
    protected abstract void initPresenters();

    /**
     * 设置标题
     *
     * @param id 标题的资源id
     */
    public void setTitle(int id) {
        mTitle.setText(id);
    }

    /**
     * 设置标题
     *
     * @param str 标题字符串
     */
    public void setTitle(String str) {
        mTitle.setText(str);
    }

    /**
     * 点击menu菜单事件
     *
     * @return
     */
    protected abstract void doActionMenu();

    protected abstract boolean hideActionBar();

    protected abstract Drawable getActionMenuIcon();

    protected abstract String getActionMenuText();



}
